package com.young.controller;

import com.young.config.SysConfig;
import com.young.entity.Club;
import com.young.entity.ClubType;
import com.young.entity.User;
import com.young.exception.NoDataException;
import com.young.service.ClubService;
import com.young.service.ClubTypeService;
import com.young.tool.PageData;
import com.young.tool.TableParam;
import com.young.tool.UploadImgTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 描述:
 *
 * @author BluceYoung
 * @create 2020-12-11 11:31
 * B站：TeacherYoung凌风
 * www.teacheryoung.com
 */
@Controller
@RequestMapping("/club")
public class ClubController {

    @Autowired
    ClubService clubService;

    @Autowired
    ClubTypeService clubTypeService;

    @GetMapping("/add.html")
    public String add(Model model){
        //读取社团分类
        List<ClubType> clubTypes = clubTypeService.listTypes();
        model.addAttribute("types",clubTypes);
        return "club/add";
    }

    @PostMapping("/add.html")
    public String doadd(Club club, String imgData, HttpSession session, Model model,
                        @SessionAttribute(SysConfig.SESSION_USER)User user){
        try {
            String savePath = session.getServletContext().getRealPath("/WEB-INF/statics/images/clublogo");
            clubService.createNewClub(club,savePath,imgData.split(",")[1]);
            model.addAttribute("msg","创建成功");
            user.setClub(club);
            session.setAttribute(SysConfig.SESSION_USER,user);
        }catch (Exception ex){
            model.addAttribute("msg","创建失败，"+ex.getMessage());
            //读取社团分类
            List<ClubType> clubTypes = clubTypeService.listTypes();
            model.addAttribute("types",clubTypes);
            model.addAttribute("club",club);
        }
        return "club/add";
    }

    @GetMapping("/edit.html")
    public String edit(Model model,
                       @SessionAttribute(SysConfig.SESSION_USER) User user){
        //读取社团分类
        List<ClubType> clubTypes = clubTypeService.listTypes();
        model.addAttribute("types",clubTypes);
        //把当前登录用户的社团对象放入model
        model.addAttribute("club",user.getClub());
        return "club/add";
    }

    @PostMapping("/edit.html")
    public String doedit(Club club,
                         Model model,
                         String imgData,//logo数据
                         @SessionAttribute(SysConfig.SESSION_USER)User user,
                         HttpSession session){
        try {
            String savePath = session.getServletContext().getRealPath("/WEB-INF/statics/images/clublogo");
            UploadImgTool.generateImg(imgData.split(",")[1],savePath,club.getId()+".jpg");
            club.setLogo(club.getId()+".jpg?r="+(new Date()).getTime());
            clubService.save(club);
            model.addAttribute("msg","修改成功");
            //更新session信息
            user.setClub(club);
            session.setAttribute(SysConfig.SESSION_USER,user);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg","修改失败，"+e.getMessage());
            //读取社团分类
            List<ClubType> clubTypes = clubTypeService.listTypes();
            model.addAttribute("types",clubTypes);
        }
        return "club/add";
    }

    @GetMapping(value = {"/info/{clubId}","/info/"})
    public String info(@PathVariable(value = "clubId",required = false) Long clubId,
                       Model model,
                       @SessionAttribute(SysConfig.SESSION_USER) User user){

        if(clubId!=null){
            //查一个社团信息
            Club condition=new Club();
            condition.setId(clubId);
            Club club = clubService.get(condition);
            if(club==null){//没有此社团
                throw new NoDataException("此社团不存在");
            }else {
                model.addAttribute("club", club);
            }
        }else{
            //自己的社团
            model.addAttribute("club",user.getClub());
        }

        return "club/info";
    }

    @GetMapping("/list.html")
    public String list(Model model){
        //读取社团分类
        List<ClubType> clubTypes = clubTypeService.listTypes();
        model.addAttribute("types",clubTypes);
        return "club/list";
    }

    @ResponseBody
    @PostMapping("/list.json")
    public PageData<Club> dolist(@RequestBody TableParam<Club> param){

        PageData<Club> clubs = clubService.listClubs(param.getObj(),
                param.getPageNumber(),
                param.getPageSize(),
                param.getSortName(),
                param.getSortOrder());
        return clubs;
    }
}
